﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Thread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void search1_Click(object sender, EventArgs e)
        {

            try
            {           
                string url_Bing = "https://cn.bing.com/search?q=" + KeyWord.Text;                                                                                                          
                string url_360 = "https://www.so.com/s?ie=utf-8&fr=none&src=360sou_newhome&ssid=&q=" + KeyWord.Text;
                Parallel.Invoke(
                    () => searchBing(url_Bing), () => search360(url_360));                                   
            }          //Parallel.Invoke方法实现任务并行
            catch (Exception ece)
            {
                MessageBox.Show("查询失败");
            }      
        }

        public void searchBing(string url)
        {
            WebClient client = new WebClient();
            byte[] pageData = client.DownloadData(url);
            String pageHtml = Encoding.UTF8.GetString(pageData);   //获取网页内容
            string h = @"[\u4e00-\u9fa5]+";  //正则表达式筛选汉字
            MatchCollection res = Regex.Matches(pageHtml, h);
            this.BeginInvoke(new ThreadStart(() => ShowBingResult(res)));  
        }
        public void search360(string url)
        {
            WebClient client = new WebClient();
            byte[] pageData = client.DownloadData(url);
            String pageHtml = Encoding.UTF8.GetString(pageData);
            string h = @"[\u4e00-\u9fa5]+";  //显示汉字
            MatchCollection res = Regex.Matches(pageHtml, h);
            this.BeginInvoke(new ThreadStart(() => Show360Result(res)));    //界面线程
        }
 
        
        private void ShowBingResult(MatchCollection a)   
        {
            try
            {
                for (int i = 0; i < 200; i++)
                {
                    bing_result.Text += a[i].ToString();
                }
            }
            catch(Exception ee)
            {
                MessageBox.Show("查询失败");
            }
        }
        private void Show360Result(MatchCollection a)       
        {
            try
            {
                for (int i = 0; i < 200; i++)
                {
                    baidu_result.Text += a[i].ToString();
                }
            }
            catch(Exception ee)
           {
               MessageBox.Show("搜索失败");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
